package com.yundong.gongchengji.util;

import android.os.Handler;
import android.os.Message;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by ww on 2016/5/29.
 */
public class TimeTools {

    private static getAllTime getTime;
    private static long time;

    //根据long型的数据获取时间值
    public static String getNormalTime(long value) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(new Date(value));
        return time;
    }

    /**
     * @param time
     * @return
     */
    public static String appendTrim(String time) {
        StringBuilder sb = new StringBuilder(time);
        sb.insert(4, "-");
        sb.insert(7, "-");
        return sb.toString();
    }

    /**
     * 获取年月日时间
     * @param value
     * @return
     */
    public static String getYMDTime(long value) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String time = format.format(new Date(value));
        return time;
    }

    /**
     * 获取时分秒
     * @param ms
     * @return
     */
    public static String getTimeHMS(long ms){
        SimpleDateFormat sdr = new SimpleDateFormat("HH:mm:ss");
        String times = sdr.format(ms);
        return times;
    }

    /**
     * 获取前50年至今
     * @return
     */
    public static ArrayList<String> getYears() {
        Calendar c = Calendar.getInstance();
        int currentYears = c.get(Calendar.YEAR);
        int lastYears = currentYears - 50;
        ArrayList<String> list = new ArrayList<>();
        for (int i = currentYears; i >= lastYears; i--) {
            list.add(String.valueOf(i));
        }
        return list;
    }

    /**
     * 获取12月
     * @return
     */
    public static ArrayList<String> getMonths(int years) {
        Calendar c = Calendar.getInstance();
        int currentYears = c.get(Calendar.YEAR);
        int count = 12;
        if (currentYears == years) {
            count = c.get(Calendar.MONTH) + 1;
        }
        ArrayList<String> list = new ArrayList<>();
        for (int i = 1; i <= count; i++) {
            if (i < 10) {
                list.add(String.valueOf("0" + i));
            } else {
                list.add(String.valueOf(i));
            }
        }
        return list;
    }

    public static ArrayList<String> getDays(int year, int month) {
        Calendar c = Calendar.getInstance();
        int currentYears = c.get(Calendar.YEAR);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        if (year == 0) {
            return calculationDays(currentYears, currentMonth, 0);
        } else {
            if (year == currentYears && currentMonth == month) {
                return calculationDays(year, month, day);
            } else {
                return calculationDays(year, month, 0);
            }
        }
    }

    /**
     * 获取活动结束年限
     * 只可能今年往后
     */
    public static ArrayList<String> getEndTimeYear() {
        Calendar c = Calendar.getInstance();
        int currentYears = c.get(Calendar.YEAR);
        int lastYears = currentYears + 5;
        ArrayList<String> list = new ArrayList<>();
        for (int i = currentYears; i <= lastYears; i++) {
            list.add(String.valueOf(i));
        }
        return list;
    }

    /**
     * 获取从该月开始的 往后时间
     * @return
     */
    public static ArrayList<String> getEndTimeMonths(int years) {
        Calendar c = Calendar.getInstance();
        int currentYears = c.get(Calendar.YEAR);
        int count = 1;
        if (currentYears == years) {
            count = c.get(Calendar.MONTH) + 1;
        }
        ArrayList<String> list = new ArrayList<>();
        for (int i = count; i <= 12; i++) {
            if (i < 10) {
                list.add(String.valueOf("0" + i));
            } else {
                list.add(String.valueOf(i));
            }
        }
        return list;
    }

    /**
     * 获取从今天开始的往后时间
     * @param year
     * @param month
     * @return
     */
    public static ArrayList<String> getEndTimeDays(int year, int month) {
        ArrayList<String> list = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        int currentYears = c.get(Calendar.YEAR);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        if (year == 0 || (year == currentYears && currentMonth == month)) {
            int days = calculationDays(currentYears, currentMonth);
            for (int i = day; i <= days; i++) {
                if (i < 10) {
                    list.add(String.valueOf("0" + i));
                } else {
                    list.add(String.valueOf(i));
                }
            }
        } else {
            int days = calculationDays(currentYears, currentMonth);
            for (int i = 1; i <= days; i++) {
                if (i < 10) {
                    list.add(String.valueOf("0" + i));
                } else {
                    list.add(String.valueOf(i));
                }
            }
        }
        return list;
    }

    /**
     * 获取某年某月天数   如果真是当前年和月 则只显示到该天的日期
     * @param year
     * @param month
     * @param toDays
     * @return
     */
    public static ArrayList<String> calculationDays(int year, int month, int toDays) {
        ArrayList<String> list = new ArrayList<>();
        if (toDays != 0) {
            for (int i = 1; i <= toDays; i++) {
                if (i < 10) {
                    list.add(String.valueOf("0" + i));
                } else {
                    list.add(String.valueOf(i));
                }
            }
            return list;
        } else {
            int days = calculationDays(year, month);
            for (int i = 1; i <= days; i++) {
                if (i < 10) {
                    list.add(String.valueOf("0" + i));
                } else {
                    list.add(String.valueOf(i));
                }
            }
            return list;
        }
    }

    public static int calculationDays(int year, int month) {
        int days = 0;
        if (month != 2) {
            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    days = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    days = 30;
            }
        } else {
            //闰年
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                days = 29;
            else
                days = 28;
        }
        return days;
    }

    //获取24小时列表
    public static ArrayList<String> getHours(){
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            if(i < 10) {
                list.add("0"+i);
            } else {
                list.add(""+i);
            }
        }
        return list;
    }

    //获取60分钟列表
    public static ArrayList<String> getMinute(){
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            if(i < 10) {
                list.add("0"+i);
            } else {
                list.add(""+i);
            }
        }
        return list;
    }



    public static String friendlyTime(long time) {
        //获取time距离当前的秒数
        int ct = (int) ((System.currentTimeMillis() - time) / 1000);

        if (ct == 0) {
            return "刚刚";
        }

        if (ct > 0 && ct < 60) {
            return ct + "秒前";
        }

        if (ct >= 60 && ct < 3600) {
            return Math.max(ct / 60, 1) + "分钟前";
        }
        if (ct >= 3600 && ct < 86400)
            return ct / 3600 + "小时前";
        if (ct >= 86400 && ct < 2592000) { //86400 * 30
            int day = ct / 86400;
            return day + "天前";
        }
        if (ct >= 2592000 && ct < 31104000) { //86400 * 30
            return ct / 2592000 + "月前";
        }
        return "刚刚";
    }

    public void startTime(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    sleep(1000);
                    handler.sendEmptyMessage(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==1){
                time+=1000;
                getTime.tims(getTimeHMS(time));
            }
            super.handleMessage(msg);
        }
    };

    public static void setGetAllTime(getAllTime time){
        getTime = time;
    }

    public interface getAllTime{
        void tims(String time);
    }

    /**
     * 返回计时时间
     * @param time
     * @return
     */
    public static String getNewTiming(long time){
        int day= (int) (time/86400000);
        int hour= (int) ((time-day*86400000)*24/86400000);
        int minus= (int) ((time-day*86400000-hour*86400000/24)*24*60/86400000);
        int seconds = (int) ((time-day*86400000-hour*86400000/24-minus*60000)*24*60*60/86400000);
        StringBuilder stringBuilder=new StringBuilder();
        if (day<10){
            stringBuilder.append("0"+day+"天");
        }else {
            stringBuilder.append(day+"天");
        }
        if (hour<10){
            stringBuilder.append("0"+hour+"时");
        }else {
            stringBuilder.append(hour+"时");
        }
        if (minus<10){
            stringBuilder.append("0"+minus+"分");
        }else {
            stringBuilder.append(minus+"分");
        }

        if (seconds<10){
            stringBuilder.append("0"+seconds+"秒");
        }else {
            stringBuilder.append(seconds+"秒");
        }
        return stringBuilder.toString();
    }
}
